package com.example.demo.service.impl;

import com.example.demo.service.OrderSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * 指令处理工厂
 * @author kisofdeath
 * @date 2022/5/11 13:21
 */
@Service
public class OrderSubFactory {
	@Autowired
	private ApplicationContext context;

	private enum OrderSub {
		/**
		 *
		 */
		LOL("英雄联盟", LolOrderSubServiceImpl.class);

		private String channel;

		private Class<? extends OrderSubService> service;

		OrderSub(String channel, Class<? extends OrderSubService> service) {
			this.channel = channel;
			this.service = service;
		}

		public String getChannel() {
			return channel;
		}

		public Class<?> getService() {
			return service;
		}

		public static OrderSub of(String channel){
			for (OrderSub value : values()) {
				if(Objects.equals(value.channel, channel)){
					return value;
				}
			}
			return null;
		}
	}

	public OrderSubService of(String channel) {
		return Optional.ofNullable(OrderSub.of(channel)).map(item -> (OrderSubService)context.getBean(item.getService())).orElse(null);
	}
}
